package com.ruoyi.manage.service.impl;

import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.manage.CL253Api.impl.SendMessageClient;
import com.ruoyi.manage.CL253Api.impl.SendVoiceClient;
import com.ruoyi.manage.CL253Api.request.SendMessageRequest;
import com.ruoyi.manage.CL253Api.request.SendVoiceRequest;
import com.ruoyi.manage.CL253Api.response.SendMessageResponse;
import com.ruoyi.manage.CL253Api.response.SendVoiceResponse;
import com.ruoyi.manage.domain.Jurisdiction;
import com.ruoyi.manage.domain.TypeTemplate;
import com.ruoyi.manage.mapper.JurisdictionMapper;
import com.ruoyi.manage.mapper.TypeTemplateMapper;
import com.ruoyi.manage.service.ISendMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: wwy
 * @Date: 2021/3/4 16:50
 */
@Service
public class SendMessageServiceImpl implements ISendMessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SendMessageServiceImpl.class);
    private static final int SEND_STAUS= 1;
    @Autowired
    private TypeTemplateMapper typeTemplateMapper;
    @Autowired
    private JurisdictionMapper jurisdictionMapper;
    /**
     * 发送通知消息
     * 1.查询出数据，判断是否需要发送
     * @param illegalType
     */
    @Override
    public void sendMessage(Integer illegalType,String jurisdictionName) {
        LOGGER.info("语音短信发送通知进入===================参数:{},{}",illegalType,jurisdictionName);
        TypeTemplate typeTemplate = typeTemplateMapper.findTypeTemplateByIllegalType(illegalType);
        Integer messageInformStatus = typeTemplate.getMessageInformStatus();
        Integer voiceInformStatus = typeTemplate.getVoiceInformStatus();
        Jurisdiction jurisdiction = jurisdictionMapper.findJurisdictionByJurisdictionName(jurisdictionName);
        String phone = jurisdiction.getPhone();
        if(messageInformStatus == SEND_STAUS){
            String messageContent = typeTemplate.getMessageContent();
            //发送短信
            SendMessageClient sendMessageClient = new SendMessageClient();
            SendMessageRequest sendMessageRequest = new SendMessageRequest();
            sendMessageRequest.setMessage(messageContent);
            sendMessageRequest.setPhone(phone);
            SendMessageResponse sendMessageResponse = sendMessageClient.sendMessageExecute(sendMessageRequest);
            if(sendMessageResponse.isSuccess()){
                //成功
                LOGGER.info("短信发送成功：号码：{}，内容：{}",phone,messageContent);
            }else{
                LOGGER.info("短信发送失败：号码：{}，内容：{}",phone,messageContent);
            }
        }

        if(voiceInformStatus == SEND_STAUS){
            String templateId = DictUtils.getDictValue("voice_push_config", "voice_push_template_id");
            //发送语音
            SendVoiceClient sendVoiceClient = new SendVoiceClient();
            SendVoiceRequest sendVoiceRequest = new SendVoiceRequest();
            sendVoiceRequest.setPhone(phone);
            sendVoiceRequest.setTemplateId(templateId);
            SendVoiceResponse sendVoiceResponse = sendVoiceClient.sendVoiceExecute(sendVoiceRequest);
            if(sendVoiceResponse.isSuccess()){
                //成功
                LOGGER.info("语音发送成功：号码：{}",phone);
            }else{
                LOGGER.info("语音发送失败：号码：{}",phone);
            }
        }
    }
}
